﻿using Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Common
{
    public class UploadHelper
    {
        /// <summary>
        /// 原始图片上传文件路径
        /// </summary>
        public static string uploadPath = "/App_Data/upfile/";

        /// <summary>
        /// 缩略图图片上传文件路径
        /// </summary>
        public static string thumbnailPath = "/App_Data/upfile/thumbnail/";

        /// <summary>
        /// 模版文件上传地址
        /// </summary>
        public static string excelPath = "/App_Data/upfile/excel/";

        /// <summary>
        /// 用户头像图片上传文件路径
        /// </summary>
        public static string avatarPath = "/App_Data/avatar/";

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="postedFile">文件流</param>
        /// <returns>上传后文件信息</returns>
        public static FileData SaveFilePic(HttpPostedFile postedFile, string path, string name)
        {
            try
            {
                int fileSize = postedFile.ContentLength; //获得文件大小，以字节为单位
                Random rd = new Random();
                var number = rd.Next(1, 10000);
                //string fileName = name + number + "_" + postedFile.FileName.Replace("&", "").Replace("\\", "").Replace("<", "").Replace("%", "").Replace("\"", "").Replace(">", "").Replace(":", "").Replace("+", "").Replace("-", "").Replace("(", "").Replace(")", "").Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "").Replace(",", "").Replace("'", "").Replace("'", "");
                string fileName = name + number + "_" + SanitizeFileName(postedFile.FileName);
                string fullUpLoadPath = Utils.GetMapPath(path); //上传目录的物理路径
                string newFilePath = path + "/" + fileName; //上传后的路径
                //检查上传的物理路径是否存在，不存在则创建
                if (!Directory.Exists(fullUpLoadPath))
                {
                    Directory.CreateDirectory(fullUpLoadPath);
                }
                //保存文件
                postedFile.SaveAs(Path.Combine(fullUpLoadPath, fileName));
                // 将信息保存到文件类中
                FileData data = new FileData();
                data.FileName = fileName;
                //data.type = fileType;
                data.FileSize = fileSize;
                data.FilePath = newFilePath;
                data.RelationId = 0;
                data.DeleteType = "";
                data.DeletePath = "";
                data.ThumbnailPath = "";
                data.IsDelete = 0;
                return data;
            }
            catch (Exception)
            {
                return null;
            }
        }

        // 辅助方法，用于清理文件名中的非法字符
        private static string SanitizeFileName(string fileName)
        {
            // 使用Path.GetInvalidFileNameChars()来获取所有非法字符，并替换或移除它们
            string invalidChars = new string(Path.GetInvalidFileNameChars());
            foreach (char c in invalidChars)
            {
                fileName = fileName.Replace(c.ToString(), "");
            }
            return fileName;
        }
    }
}